package com.yange.accountbook.utils

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.yange.accountbook.R


/**
 *
 * @function Title栏
 * Created by yange
 */
class TitleBarView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {


    var tvTitle: TextView? = null;
    var tvForward: TextView? = null;
    var tvBack: TextView? = null;

    init {
        LayoutInflater.from(context).inflate(R.layout.title_bar, this, true)
        tvBack = findViewById(R.id.tv_back)
        tvForward = findViewById(R.id.tv_forward)
        tvTitle = findViewById(R.id.tv_title)
        initView()
        val typedArray =
            context.obtainStyledAttributes(attrs, R.styleable.TitleBarView, defStyleAttr, 0)
        tvBack?.text = typedArray.getString(R.styleable.TitleBarView_backText)
        tvBack?.setTextColor(
            typedArray.getColor(
                R.styleable.TitleBarView_mBackTextColor,
                resources.getColor(R.color.text_black)
            )
        )
        tvBack?.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            typedArray.getDimension(R.styleable.TitleBarView_backTextSize, 0f)
        )

        tvTitle?.text = typedArray.getString(R.styleable.TitleBarView_titleText)
        tvTitle?.setTextColor(
            typedArray.getColor(
                R.styleable.TitleBarView_mTitleTextColor,
                resources.getColor(R.color.text_black)
            )
        )
        tvTitle?.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            typedArray.getDimension(R.styleable.TitleBarView_titleTextSize, 0f)
        )

        tvForward?.text = typedArray.getString(R.styleable.TitleBarView_forwardText)
        tvForward?.setTextColor(
            typedArray.getColor(
                R.styleable.TitleBarView_mForwardTextColor,
                resources.getColor(R.color.text_black)
            )
        )
        tvForward?.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            typedArray.getDimension(R.styleable.TitleBarView_forwardTextSize, 0f)
        )

        var drawable = typedArray.getDrawable(R.styleable.TitleBarView_backDrawable)
        if (drawable != null) {
            tvBack?.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
        }
        drawable = typedArray.getDrawable(R.styleable.TitleBarView_forwardDrawable)
        if (drawable != null) {
            tvForward?.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null)
        }
        typedArray.recycle()
    }


    fun setTitle(title: String) {
        tvTitle?.text = title
    }

    fun setForword(forward: String) {
        tvForward?.text = forward
    }

    private fun initView() {
        tvBack?.setOnClickListener {
            if (context is Activity) {
                (context as Activity).onBackPressed()
            }
        }
    }

    fun setForwardGone() {
        tvForward?.visibility = View.GONE
    }

    fun setForwardVisible() {
        tvForward?.visibility = View.VISIBLE
    }

    fun setBackGone() {
        tvBack?.visibility = View.GONE
    }

}
